package net.fei.scan.quick.ui

import android.graphics.Bitmap
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.util.DisplayMetrics
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.constraintlayout.widget.ConstraintLayout
import com.google.gson.Gson
import com.google.zxing.BarcodeFormat
import com.google.zxing.EncodeHintType
import com.google.zxing.WriterException
import com.google.zxing.qrcode.QRCodeWriter
import net.fei.scan.quick.R
import net.fei.scan.quick.databinding.ActivityUserOractivityBinding
import net.fei.scan.quick.entity.History
import net.fei.scan.quick.util.BitMapUtil
import net.fei.scan.quick.util.mmkv
import java.util.UUID

class UserORActivity : AppCompatActivity() {
    private lateinit var bin: ActivityUserOractivityBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bin = ActivityUserOractivityBinding.inflate(layoutInflater)
        setContentView(bin.root)
        initializeData()
        calculateWidth()
        getData()
        bin.submit.setOnClickListener {
            setData()
        }
    }

    private fun initializeData() {
        val string = mmkv.getString("user")
        if (!string.isNullOrBlank()) {
            bin.editLayout.visibility = View.GONE
            generateQRCode(string)
        }
    }

    private fun getData() {
    }

    private fun calculateWidth() {
        val displayMetrics = DisplayMetrics()
        windowManager.defaultDisplay.getMetrics(displayMetrics)
        val screenWidth = displayMetrics.widthPixels
        val layoutWidth = (screenWidth * 0.9).toInt()
        val layoutParams = bin.layout.layoutParams as ConstraintLayout.LayoutParams
        layoutParams.width = layoutWidth
        layoutParams.height = layoutWidth
        bin.layout.layoutParams = layoutParams
    }

    private fun setData() {
        bin.apply {
            val text = name.text.toString()
            val text1 = company.text.toString()
            val text2 = address.text.toString()
            val text3 = phone.text.toString()
            val text4 = node.text.toString()

            if (text.isNullOrBlank() || text1.isNullOrBlank() || text2.isNullOrBlank() || text3.isNullOrBlank() || text4.isNullOrBlank()) {
                Toast.makeText(this@UserORActivity, getString(R.string.fdkjdlskghfl), Toast.LENGTH_LONG).show()
                return@apply
            }
            val content = String.format(
                "BEGIN:VCARD\n" + "VERSION:3.0\n" + "N:%s\n" + "ORG:%s\n" + "ADR:%s\n" + "TEL:%s\n" + "NOTE:%s\n" + "END:VCARD",
                text,
                text1,
                text2,
                text3,
                text4
            )

            mmkv.saveString("user", content)
            generateQRCode(content)

        }
    }

    private fun generateQRCode(content: String) {
        bin.layout.visibility = View.VISIBLE
        bin.editLayout.visibility=View.GONE
        val generateBitmap = BitMapUtil.generateBitmap(content, 200, 200)
        Log.d("TAG", "$generateBitmap")
        bin.ma.setImageBitmap(generateBitmap)
        bin.btnLayout.visibility = View.VISIBLE
        bin.saveUser.setOnClickListener {
            val uri = BitMapUtil.saveBitmapToGallery(
                this@UserORActivity,
                generateBitmap!!,
                UUID.randomUUID().toString(),
                Environment.DIRECTORY_DCIM
            )
            if (uri != null) {
                Toast.makeText(this, getString(R.string.lkdsfjlds), Toast.LENGTH_LONG).show()
            } else {
                Toast.makeText(this, getString(R.string.jkfdsskjfgbs), Toast.LENGTH_LONG).show()
            }
        }
        bin.edit.setOnClickListener {
            mmkv.saveString("user","")
            recreate()
        }
    }

}